package com.seal.commons.utils.thread;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:ThreadContext <br/>
 * Date:     2019-04-22 17:21 <br/>
 *
 * @author yong.huang
 * @see
 * @since JDK 1.8
 */
public class ThreadContext {

    private Map<Object,Object> data = new HashMap<>();

    public void put(Object key, Object message){
        data.put(key, message);
    }

    public Object get(Object key){
        return data.get(key);
    }

    public <T> T get(Object key,Class<T> clazz){
        return (T) data.get(key);
    }

    /**
     * 获取线程上下文
     * @return
     */
    public static ThreadContext getContext(){
        return threadContext.get();
    }


    public static void clear(){
        threadContext.remove();
    }


    private static ThreadLocal<ThreadContext> threadContext = new ThreadLocal<ThreadContext>() {
        protected ThreadContext initialValue() {
            return new ThreadContext();
        }
    };

}
